package com.twu.biblioteca;

import java.io.IOException;
import java.util.List;

/**
 * Created by sahelig on 12/06/14.
 */
public class ListBooks implements MenuAction {
    private Library libraryOnWhichOperationsArePerformed;
    private String stringToBeReturned;
    private boolean successful;
    private String actionPerformed;

    public ListBooks(Library lib)
    {

        libraryOnWhichOperationsArePerformed = lib;
        actionPerformed ="to List Books";
    }


    public Result performAction(UserInterface ui,String libraryNumber)throws IOException
    {


        List<Book> availableBooks = libraryOnWhichOperationsArePerformed.getAvailableBooks();

        int len = availableBooks.size();

        if (len == 0)
        {
            ui.display("No books in library at present");
            stringToBeReturned = "No books in library at present";
            successful = false;
        }
        else
        {
            successful = true;
            stringToBeReturned="Books are available in the library";
            ui.display("The available books are:");
            for (int i = 0; i < len; i++)
            {
                ui.displayBook(availableBooks.get(i));

            }
        }

        return new Result(successful, stringToBeReturned);
    }

    public String toString()
    {
        return actionPerformed;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ListBooks listBooks = (ListBooks) o;

        if (successful != listBooks.successful) return false;
        if (actionPerformed != null ? !actionPerformed.equals(listBooks.actionPerformed) : listBooks.actionPerformed != null)
            return false;
        if (libraryOnWhichOperationsArePerformed != null ? !libraryOnWhichOperationsArePerformed.equals(listBooks.libraryOnWhichOperationsArePerformed) : listBooks.libraryOnWhichOperationsArePerformed != null)
            return false;
        if (stringToBeReturned != null ? !stringToBeReturned.equals(listBooks.stringToBeReturned) : listBooks.stringToBeReturned != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = libraryOnWhichOperationsArePerformed != null ? libraryOnWhichOperationsArePerformed.hashCode() : 0;
        result = 31 * result + (stringToBeReturned != null ? stringToBeReturned.hashCode() : 0);
        result = 31 * result + (successful ? 1 : 0);
        result = 31 * result + (actionPerformed != null ? actionPerformed.hashCode() : 0);
        return result;
    }
}
